package com.hzjy.api.controller;

import com.github.pagehelper.PageInfo;
import com.hzjy.api.beans.HzjyAgreement;
import com.hzjy.api.service.BqTokenService;
import com.hzjy.api.service.impl.ApiProductServiceImpl;
import com.hzjy.api.service.impl.HzjyAgreementServiceImpl;
import com.hzjy.api.vo.BqLoginUser;
import com.hzjy.common.constant.HttpStatus;
import com.hzjy.common.core.controller.BaseController;
import com.hzjy.common.core.domain.AjaxResult;
import com.hzjy.common.core.page.TableDataInfo;
import com.hzjy.common.utils.PageData;
import com.hzjy.common.utils.ServletUtils;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 协议书，合同Controller
 *
 * @author hzjy
 * @date 2020-08-26
 */
@RestController
@RequestMapping("/api/agreement")
public class HzjyAgreementController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(HzjyAgreementController.class);
    @Autowired
    private HzjyAgreementServiceImpl hzjyAgreementService;
    @Autowired
    private BqTokenService bqTokenService;
    @Autowired
    private ApiProductServiceImpl apiProductService;

    /**
     * 查询协议书，合同列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody HzjyAgreement hzjyAgreement) {
        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
        if (loginUser == null) {
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.ERROR);
            rspData.setMsg("未登录");
            return rspData;
        }
        hzjyAgreement.setUserId(loginUser.getBqUser().getUserId().toString());
        hzjyAgreement.setSaleUserId(loginUser.getBqUser().getUserId().toString());

        startPage();
        List<HzjyAgreement> list = hzjyAgreementService.selectHzjyAgreementList(hzjyAgreement);
        return getDataTable(list);
    }

//    /**
//     * 查询协议书，合同列表
//     */
//    @ApiOperation("获取合同列表")
//    @PostMapping("/list")
//    public AjaxResult list(@RequestBody PageData pageDomain) {
//        log.info("【查询合同列表】,args:" + JSONUtil.map2json(pageDomain));
//        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
//        if (loginUser == null) {
//            return AjaxResult.error(401, "未登录");
//        }
//        Integer pageNum = Integer.parseInt(pageDomain.get("pageNum").toString());
//        Integer pageSize = Integer.parseInt(pageDomain.get("pageSize").toString());
//        PageResultVo dataInfo = hzjyAgreementService.selectContractListForPage(pageNum, pageSize, pageDomain);
//        log.info("【查询合同列表结果】" + JSONUtil.object2json(dataInfo));
//        return AjaxResult.success(200, "查询成功", dataInfo);
//    }

    @ApiOperation("挂牌方确认收款")
    @PostMapping("/confirmTradingPay")
    public AjaxResult confirmTradingPay(@RequestBody PageData pd) {
        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
        if (loginUser == null) {
            return AjaxResult.error(401, "未登录");
        }
        String contractId = pd.get("contractId").toString();
        return apiProductService.confirmTradingPay(contractId);
    }

    @ApiOperation("摘牌方确认收货")
    @PostMapping("/confirmDelistingPay")
    public AjaxResult confirmDelistingPay(@RequestBody PageData pd) {
        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
        if (loginUser == null) {
            return AjaxResult.error(401, "未登录");
        }
        String contractId = pd.get("contractId").toString();
        return apiProductService.confirmDelistingPay(contractId);
    }

    @ApiOperation("下载合同")
    @GetMapping("/downContractFromEQB")
    public AjaxResult downContractFromEQB(String flowId) throws Exception {
        String url = hzjyAgreementService.downContractFromEQB(flowId);
        return AjaxResult.success(200,"成功",url);
    }

    /**
     * wx合同列表
     */
    @PostMapping("/wxList")
    public AjaxResult wxList(@RequestBody PageData pageData) {
        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
        if (loginUser == null) {
            return AjaxResult.error(401,"未登录");
        }
        pageData.put("userId",loginUser.getBqUser().getUserId().toString());
        pageData.put("saleUserId",loginUser.getBqUser().getUserId().toString());
        //hzjyAgreement.setUserId(loginUser.getBqUser().getUserId().toString());
        //hzjyAgreement.setSaleUserId(loginUser.getBqUser().getUserId().toString());
        if(pageData.isEmpty()){
            pageData.put("currentPage",0);
            pageData.put("size",10);
        }else{
            pageData.put("currentPage",(Integer.parseInt(pageData.get("currentPage").toString())-1) * Integer.parseInt(pageData.get("size").toString()));
        }
        List<PageData> pageDataList = hzjyAgreementService.queryHzjyAgreementList(pageData);
        PageData count = hzjyAgreementService.getAgreementNum(pageData);
        Map map = new HashMap();
        map.put("agreementList", pageDataList);
        map.put("totalResult", count.get("totalResult"));
        return AjaxResult.success(200,"查询成功",map);
    }
}
